package com.springboot.util;

import com.springboot.model.DoctorAppointSet;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.ServletOutputStream;
import java.util.ArrayList;

public class ExcelUtil {
    public static void exportExcelUsers(ArrayList<DoctorAppointSet> doctorAppointSets, ServletOutputStream outputStream) throws Exception {
        //1.创建工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();

        // 1.1创建合并单元格
        CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 6);

        // 1.2创建标题样式
        CellStyle style1 = createStyle(workbook, 16);
        // 1.3创建列标题样式
        CellStyle style2 = createStyle(workbook, 12);
        // 2.创建工作表
        Sheet sheet = workbook.createSheet("hello world");
        // 设置默认列宽
        sheet.setDefaultColumnWidth(15);
        // 2.1将合并单元格作用于sheet
        sheet.addMergedRegion(cra);
        // 3.创建行
        Row row = sheet.createRow(0);
        // 4.创建单元格
        Cell cell = row.createCell(0);
        // 5.向单元格写入数据
        cell.setCellValue("用户列表");
        // 6.将单元格加入样式
        cell.setCellStyle(style1);

        // 写列标题
        String[] titles = {"医生编号", "预约日期", "上午人数", "下午人数"};
        // 创建列标题行
        Row row2 = sheet.createRow(1);
        for (int i = 0; i < titles.length; i++) {
            // 创建单元格
            Cell cell1 = row2.createCell(i);
            // 向单元格写入数据
            cell1.setCellValue(titles[i]);
            // 将单元格加入样式
            cell1.setCellStyle(style2);
        }

        for (int i = 0; i < doctorAppointSets.size(); i++) {
            DoctorAppointSet doctorAppointSet = doctorAppointSets.get(i);
            Row rowdata = sheet.createRow(i + 2);
            // 姓名
            Cell cell0 = rowdata.createCell(0);
            cell0.setCellValue(doctorAppointSet.getDoctorId());
            // 账号
            Cell cell1 = rowdata.createCell(1);
            cell1.setCellValue(doctorAppointSet.getBookDate());
            // 部门
            Cell cell2 = rowdata.createCell(2);
            cell2.setCellValue(doctorAppointSet.getMorningNum());
            // 性别
            Cell cell3 = rowdata.createCell(3);
            cell3.setCellValue(doctorAppointSet.getAfternoonNum());

        }

        // 6.写入硬盘文件
        workbook.write(outputStream);
        workbook.close();
    }


    //设置样式方法
    public static CellStyle createStyle(Workbook workbook, int fontsize) {
        //1.2设置单元格样式
        CellStyle style = workbook.createCellStyle();
        // 设置水平居中
        //style.setAlignment(CellStyle.ALIGN_CENTER);
        // 设置垂直居中
        //style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        // 1.3设置字体
        Font font = workbook.createFont();
        // 设置字体为ARIAL
        font.setFontName(HSSFFont.FONT_ARIAL);
        // 设置粗体
        //font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        // 设置字体颜色
        //font.setColor(HSSFColor.BLUE.index);
        // 设置字体大小
        font.setFontHeightInPoints((short) fontsize);
        // 将字体加入样式
        style.setFont(font);
        return style;
    }
}

